﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using W.Common.Enum;
using W.Warehouse.Api.Application.Command.MoveCommand;
using W.Warehouse.Api.Application.Command.WarehouseTypeCommand;
using W.Warehouse.Domain;
using W.Warehouse.Domain.AllocationProduct;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;
using static WMSpublic.ParseAPI;

namespace W.Warehouse.Api.Application.CommandHandlder.MoveCommandHadler
{
    public class MoveAddCommandHadler : IRequestHandler<MoveAddCommand, ApiResult<int>>
    {
        private readonly IMoveProductRepository moveProductRepository;
        private readonly IMoveTheLibraryRepository moveTheLibraryRepository;
        public MoveAddCommandHadler(IMoveProductRepository moveProductRepository, IMoveTheLibraryRepository moveTheLibraryRepository)
        {
            this.moveProductRepository = moveProductRepository;
            this.moveTheLibraryRepository = moveTheLibraryRepository;
        }


        public async Task<ApiResult<int>> Handle(MoveAddCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            using (var tran = moveTheLibraryRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    //移库添加一条数据
                    var now = DateTime.Now;
                    MoveTheLibrary moveTheLibrary = new MoveTheLibrary()
                    {
                        
                        MoveTheLibraryNo = request.MoveTheLibraryNo,
                        MoveTheLibraryType = request.MoveTheLibraryType,
                        AssociateNumber = request.AssociateNumber,
                        SingleMaker = request.SingleMaker,
                        MakeTime = request.MakeTime,
                        MoveRemark = request.MoveRemark,
                        AuditStatus = AuditStatus.等待审核,
                        office_id = request.office_id,
                        user_id = request.user_id,
                        ExamineReason = request.ExamineReason,
                        ExaminePeople = request.ExaminePeople,
                        ExamineTime = request.ExamineTime,
                        CreateName = request.CreateName,
                        CreateTime = DateTime.Now,
                        IsDelete = false,
            
                    };
                    await moveTheLibraryRepository.AddAsync(moveTheLibrary);
                    //给移库产品信息添加
                    foreach (var item in request.MoveProduct)
                    {
                        MoveProduct moveProducts = new MoveProduct()
                        {
                            MoveTheLibraryId = moveTheLibrary.MoveTheLibraryId,
                            MoveProductId = item.MoveProductId,
                            ProductName = item.ProductName,
                            ProductNo = item.ProductNo,
                            ProductSpec = item.ProductSpec,
                            Batch = "20241205",
                            Price = item.Price,
                            OutStorageNum = item.OutStorageNum,
                            NewBinLocationName = item.NewBinLocationName,
                            BinLocationId = item.BinLocationId,
                            //WarehouseLocationName= "仓库2:默认正式库位",
                            //WarehouseName="1号仓库",
                            IsDelete = false,
                        };
                        await moveProductRepository.AddAsync(moveProducts);
                    }
                    var list = await moveTheLibraryRepository.DB.SaveChangesAsync();
                    tran.Commit();
                    result.Code = list;
                }
                catch (Exception ex)
                {
                    result.Code = -1;
                    result.Message = ex.Message;
                    tran.Rollback();
                    throw;
                }
            }
            return result;
        }
    }
}
